#!/usr/bin/env python
# encoding: utf-8
'''
@author: Excelsiorly
@license: (C) Copyright 2021, All Rights Reserved.
@contact: excelsiorly@qq.com
@file: 0024.py
@time: 2021/9/26 14:20
@desc: https://leetcode-cn.com/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof/
'''
class Solution:
    def exchange(self, nums: list) -> list:
        if not nums:
            return nums
        i, j = 0, len(nums) - 1  # i找偶数，j找奇数
        while not i >= j:
            # 奇偶交换
            if nums[i] % 2 == 0 and not nums[j] % 2 == 0:
                t = nums[i]
                nums[i] = nums[j]
                nums[j] = t
            else:
                if not nums[i] % 2 == 0:
                    i += 1
                if nums[j] % 2 == 0:
                    j -= 1

        return nums

if __name__ == '__main__':
    nums = [1, 2, 3, 4]
    res = Solution().exchange(nums)
    print(res)